package com.rong.juc_test.juc_base.readwrite;

import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * 读写锁降级演示
 */
public class demotionDemo {
    public static void main(String[] args) {
        // 可重入读写锁对象
        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
        // 读锁
        ReentrantReadWriteLock.ReadLock readLock = lock.readLock();
        // 写锁
        ReentrantReadWriteLock.WriteLock writeLock = lock.writeLock();

        // 锁降级
        writeLock.lock(); // 获取写锁
        System.out.println("writeLock lock");

        readLock.lock();// 获取读锁
        System.out.println("readLock lock");

        writeLock.unlock();
        readLock.unlock();
    }
}
